$(document).ready(function(){
    
    $('.pooleditbtn').bind('click', function(){

        var poolid = $(this).data('poolid');

        poolInlineEdit(poolid);console.log(poolid);
        $(this).addClass('hide');
        $('#pooldeletebtn_' + poolid).addClass('hide');
        $('#poolsavebtn_' + poolid).removeClass('hide');
        $('#poolcancelbtn_' + poolid).removeClass('hide');
    });

    $('.pooldeletebtn').bind('click', function(){

        var poolid = $(this).data('poolid');

        poolInlineDelete(poolid);
    });

    $('.poolsavebtn').bind('click', function(){

        var poolid = $(this).data('poolid');

        poolInlineSave(poolid);

        $(this).addClass('hide');
        $('#poolcancelbtn_' + poolid).addClass('hide');
        $('#pooleditbtn_' + poolid).removeClass('hide');
        $('#pooldeletebtn_' + poolid).removeClass('hide');
    });

    $('.poolcancelbtn').bind('click', function(){

        var poolid = $(this).data('poolid');

        poolCancelInlineEdit(poolid);

        $(this).addClass('hide');
        $('#poolsavebtn_' + poolid).addClass('hide');
        $('#pooleditbtn_' + poolid).removeClass('hide');
        $('#pooldeletebtn_' + poolid).removeClass('hide');
    });

    $('.resumebtn').bind('click', function(){

        var poolid = $(this).data('poolid');
        poolInlineResume(poolid);
    });


    $('.filter_checkbox').bind('click',function(){
        if ($(this).is(':checked')) {
            $('.tbtr_' + $(this).data('isp')).show();
        } else {
            $('.tbtr_' + $(this).data('isp')).hide();
        }
    });

    $('#tb').bind('mousewheel', function(event, delta) {  
        theadWidth();
        $('.t_h').css('border-bottom-color',randomcolor());
    });

    theadWidth();

});


function poolInlineEdit(poolid) {

    var universe = $('#universe_' + poolid).html();
    var pool = $('#pool_' + poolid).html();
    var ip = $('#ip_' + poolid).html();

    universe = universe.replace(/\<br\>/g, ',');
    ip = ip.replace(/\<br\>/g, ',');

    $('#universe_' + poolid).html('<textarea name="universe" type="text" style="width:180px;" id="inputUniverse_' + poolid + '">' + universe + '</textarea>');
    $('#pool_' + poolid).html('<textarea name="pool" type="text" class="col-lg-12" id="inputPool_' + poolid + '">' + pool + '</textarea>');
    $('#ip_' + poolid).html('<textarea name="ip" class="col-lg-12" id="inputIp_' + poolid + '">' + ip + '</textarea>');
}


function poolCancelInlineEdit(poolid) {

    var universe = $('#inputUniverse_' + poolid).val();
    var pool = $('#inputPool_' + poolid).val();
    var ip = $('#inputIp_' + poolid).val();
    
    universe = universe.replace(/,/g, '<br>');
    ip = ip.replace(/,/g, '<br>');

    $('#universe_' + poolid).html(universe);
    $('#pool_' + poolid).html(pool);
    $('#ip_' + poolid).html(ip);
}


function poolInlineSave(poolid) {

    var universe = $('#inputUniverse_' + poolid).val();
    var pool = $('#inputPool_' + poolid).val();
    var ip = $('#inputIp_' + poolid).val();

    $.ajax({
        url: _POOLUPDATEURL_,
        data: {
            id: poolid,
            universe: universe,
            name: pool,
            ip: ip,
        },
        type: 'POST',
        success: function(rstdata){
            poolCancelInlineEdit(poolid);
            $('#poolid_' + poolid).addClass("alert alert-success");
        }
    });
}

function poolInlineDelete(poolid) {
    $.ajax({
        url: _POOLDELETEURL_,
        data: {
            id: poolid
        },
        type: 'POST',
        success: function(rstdata){
            $('#poolid_' + poolid).addClass('hide');
        }
    });
}

function poolInlineResume(poolid) {
    $.ajax({
        url: _POOLRESUMEURL_,
        data: {
            id: poolid
        },
        type: 'POST',
        success: function(rstdata){
            $('#poolid_' + poolid).addClass('hide');
        }
    });
}

function theadWidth() {
    $('#h_id').width($('.id').width());
    $('#h_universe').width($('.universe').width());
    $('#h_company').width($('.company').width());
    $('#h_tsender').width($('.tsender').width());
    $('#h_tip').width($('.tip').width());
    $('#h_csender').width($('.csender').width());
    $('#h_cip').width($('.cip').width());
    $('#h_level').width($('.level').width());
    $('#h_isp').width($('.isp').width());

    if($('#h_operation')) {
        $('#h_operation').width($('.operation').width());
    }

}
function randomcolor(){
    var str=Math.ceil(Math.random()*16777215).toString(16);   
    if(str.length<6){   
        str="0"+str;   
    }
    return '#'+str;
}